﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using AutoMapper;

namespace AutoMapperSamples.Samples
{
    [TestClass]
    public class Projection
    {
        [TestMethod]
        public void ProjectionTest()
        {
            var calendarEvent = new CalendarEvent
            {
                Date = new DateTime(2008, 12, 15, 20, 30, 0),
                Title = "Company Holiday Party"
            };

            // 初始化映射
            Mapper.Initialize(cfg =>
              cfg.CreateMap<CalendarEvent, CalendarEventForm>()
                .ForMember(dest => dest.EventDate, opt => opt.MapFrom(src => src.Date.Date))
                .ForMember(dest => dest.EventHour, opt => opt.MapFrom(src => src.Date.Hour))
                .ForMember(dest => dest.EventMinute, opt => opt.MapFrom(src => src.Date.Minute)));

            
            CalendarEventForm form = Mapper.Map<CalendarEvent, CalendarEventForm>(calendarEvent);

            Assert.AreEqual(new DateTime(2008, 12, 15), form.EventDate);
            Assert.AreEqual(20, form.EventHour);
            Assert.AreEqual(30, form.EventMinute);
            Assert.AreEqual("Company Holiday Party", form.Title);
        }
    }

    /// <summary>
    /// 源对象
    /// </summary>
    public class CalendarEvent
    {
        public DateTime Date { get; set; }
        public string Title { get; set; }
    }

    /// <summary>
    /// 目标对象
    /// </summary>
    public class CalendarEventForm
    {
        public DateTime EventDate { get; set; }
        public int EventHour { get; set; }
        public int EventMinute { get; set; }
        public string Title { get; set; }
    }


}
